<template>
  <Table
    ref="tableRef"
    :url="URL_PAGINGWHITELIST"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
  ></Table>
</template>

<script lang="ts" setup name="whiteList">
import Table from '@/components/table/index.vue';
import { URL_PAGINGWHITELIST, deleteWhite } from '@/service/apis/pile';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'name',
      label: '名称',
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  handles: [
    {
      label: '详情',
      handle: (row: any) => {
        console.log('详情 ---', row);
        router.push({
          name: 'whiteDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'whiteList.edit',
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'whiteEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'whiteList.delete',
      handle: (row: any) => {
        ElMessageBox.confirm('此操作将永久删除该数据，是否继续？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          deleteWhite({
            id: row.id,
          }).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'whiteList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'whiteAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '名称',
      prop: 'name',
      comName: 'input',
      props: {
        placeholder: '请输入名称',
      },
    },
    {
      label: '修改时间',
      prop: 'time',
      comName: 'date',
      keys: ['dateChangedStartTime', 'dateChangedEndTime'],
      props: {
        type: 'daterange',
      },
    },
  ],
});
</script>